package com.cyys.modules.system.controller.notification;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cyys.common.annotation.SystemLog;
import com.cyys.common.ql.AR;
import com.cyys.common.ql.JSONData;
import com.cyys.common.validate.Detail;
import com.cyys.common.validate.Edit;
import com.cyys.common.validate.Insert;
import com.cyys.modules.core.controller.BaseAction;
import com.cyys.modules.core.sysParam.ParamBasicUtil;
import com.cyys.modules.core.sysParam.ParamConstants;
import com.cyys.modules.system.model.notification.Notice;
import com.cyys.modules.system.service.notification.NoticeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("/sys/notice")
public class NoticeAction extends BaseAction {

	@Resource
	private NoticeService noticeService;

	@GetMapping("/list")
	@RequiresPermissions("sys:notice:list")
	public String list(Model model) {
		model.addAttribute("noticeTypeMap", ParamBasicUtil.getParamBasicTypeMap().get(ParamConstants.SYSTEM_NOTICE_TYPE));
		model.addAttribute("isShowMap", ParamBasicUtil.getParamBasicTypeMap().get(ParamConstants.IS_SHOW));
		return "module/system/notification/notice-list";
	}

	@GetMapping("/add/input")
	@RequiresPermissions("sys:notice:add")
	public String addInput(Model model)  {
		model.addAttribute("noticeTypeMap", ParamBasicUtil.getParamBasicTypeMap().get(ParamConstants.SYSTEM_NOTICE_TYPE));
		model.addAttribute("isShowMap", ParamBasicUtil.getParamBasicTypeMap().get(ParamConstants.IS_SHOW));
		return "module/system/notification/notice-input";
	}
	
	@GetMapping("/edit/input")
	@RequiresPermissions("sys:notice:edit")
	public String updateInput(@Validated(Detail.class)Notice notice, Model model)  {
		model.addAttribute("noticeTypeMap", ParamBasicUtil.getParamBasicTypeMap().get(ParamConstants.SYSTEM_NOTICE_TYPE));
		model.addAttribute("isShowMap", ParamBasicUtil.getParamBasicTypeMap().get(ParamConstants.IS_SHOW));
		model.addAttribute("notice", noticeService.getById(notice.getId()));
		return "module/system/notification/notice-input";
	}
	
	@ResponseBody
	@PostMapping("/add/save")
	@RequiresPermissions(value = "sys:notice:add")
	@SystemLog(module = "系统通知", methods = "系统通知管理-保存系统通知")
	public AR save(@Validated(Insert.class)Notice notice)  {
		return noticeService.save(notice);
	}

	@ResponseBody
	@PostMapping("/edit/save")
	@RequiresPermissions(value = "sys:notice:edit")
	@SystemLog(module = "系统通知", methods = "系统通知管理-更新系统通知")
	public AR updateSave(@Validated(Edit.class) Notice notice)  {
		return noticeService.update(notice);
	}

	@ResponseBody
	@PostMapping("/list/listInfo")
	@RequiresPermissions("sys:notice:list")
	@SystemLog(module = "系统通知", methods = "系统通知管理-系统通知列表")
	public String listInfo(Notice notice, Page page)  {
		return JSONData.toLayerData(noticeService.list(page , new QueryWrapper(notice),true));
	}
	
	@ResponseBody
	@PostMapping("/del/deleteById")
	@RequiresPermissions("sys:notice:del")
	@SystemLog(module = "系统通知", methods = "系统通知管理-删除系统通知")
	public AR deleteById(String id)  {
		return noticeService.deleteById(id);
	}

	@ResponseBody
	@PostMapping("/del/deleteByIds")
	@RequiresPermissions("sys:notice:del")
	@SystemLog(module = "系统通知", methods = "系统通知管理-批量删除系统通知")
	public AR deleteByIds(String ids)  {
		return noticeService.deleteBatchIds(ids);
	}

}
